package net.jforum.repository;

import java.util.ArrayList;
import java.util.List;

import net.jforum.cache.CacheEngine;
import net.jforum.cache.Cacheable;
import net.jforum.dao.DataAccessDriver;
import net.jforum.entities.User;
import net.jforum.util.preferences.ConfigKeys;
import net.jforum.util.preferences.SystemGlobals;
/**
 * @ author  张洋 jackson.zhang@ericsson.com
 * @ version 创建时间 ：Aug 12, 2010  1:29:47 AM
 * @ description : 
 */
public class ModeratorRepository implements Cacheable{

	private static final String FQN = "moderators";//(FQN,(groupId,user))
	private static CacheEngine cache;
	
	/**
	 * @see net.jforum.cache.Cacheable#setCacheEngine(net.jforum.cache.CacheEngine)
	 */
	public void setCacheEngine(CacheEngine engine)
	{
		cache = engine;
	}
	public static List<User> getModeratorsByGroup(int groupId){
		//超级管理员不显示在版主页面里面
	    if(groupId==SystemGlobals.getIntValue(ConfigKeys.ADMIN_GROUP_ID)){
	    	return new ArrayList();
	    }
		List users=(List) cache.get(FQN, Integer.toString(groupId));
		if(users==null||users.size()==0){
			users=loadModeratorByGroup(groupId);
		}
        return users;
	}
    public static List loadModeratorByGroup(int groupId){
    	List users=DataAccessDriver.getInstance().newUserDAO().selectAllByGroup(groupId, 0, 5);
    	cache.add(FQN, Integer.toString(groupId),users);
    	return users;
    }
}
